package com.gxuwz.controller;

import com.gxuwz.entity.Classes;
import com.gxuwz.entity.Course;
import com.gxuwz.entity.vo.CourseVo;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.Code;
import com.gxuwz.result.PageInfo;
import com.gxuwz.result.ResultInfo;
import com.gxuwz.service.ClassesService;
import com.gxuwz.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description:课程信息管理模块
 */

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private ClassesService classesService;

    /**
     * @Description:查询课程信息，并跳转至课程信息管理页面
     * @Param:  [com.gxuwz.result.PageInfo, org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Course]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/list")
    public ModelAndView courseList(PageInfo pageInfo, ModelAndView modelAndView, Course course, HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        PageVo pagevo = courseService.getCourseList(course,pageInfo,username);
        modelAndView.setViewName("courseManage/courseManage");
        modelAndView.addObject("pageVo",pagevo);
        return modelAndView;
    }

    /**
     * @Description:跳转至课程信息添加页面
     * @Param:  [org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/addCoursePage")
    public ModelAndView queryIds(ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        List<String> ids = courseService.selectCourseIds();
        System.out.println(ids);
        List<Classes> classesList = classesService.queryClassesIdAndClassName(username);
        modelAndView.addObject("classesList",classesList);
        modelAndView.addObject("courseIds",ids);
        modelAndView.setViewName("courseManage/addCourse");
        return modelAndView;
    }

    /**
     * @Description:添加单个课程信息，并跳转至课程信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Course]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/addCourse")
    public ModelAndView addDepartment(ModelAndView modelAndView, Course course,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flag = courseService.addCourseInfo(course);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_OK,"新增成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_ERR,"新增失败"));
        }
        PageVo pageVo = courseService.getCourseList(null, pageInfo,username);
        modelAndView.setViewName("courseManage/courseManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:跳转至课程信息修改页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/updateCoursePage")
    public ModelAndView queryCourseOne(String courseId,ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        CourseVo courseVo = courseService.getCourseOne(courseId);
        List<Classes> classesList = classesService.queryClassesIdAndClassName(username);
        List<String> ids = courseService.selectCourseIds();
        modelAndView.addObject("classesList",classesList);
        modelAndView.addObject("courseIds",ids);
        modelAndView.addObject("courseVo",courseVo);
        modelAndView.setViewName("courseManage/editCourse");
        return modelAndView;
    }

    /**
     * @Description:修改课程信息，并跳转至课程信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Course, java.lang.String]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/updateCourse")
    public ModelAndView updateCourse(ModelAndView modelAndView,Course course,String oldId,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(oldId);
        boolean flag = courseService.updateCourseInfo(course,oldId);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改失败"));
        }
        PageVo pageVo = courseService.getCourseList(null,pageInfo,username);
        modelAndView.setViewName("courseManage/courseManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现删除单独课程信息功能，并跳转至课程信息管理页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteCourseOne")
    public ModelAndView deleteCourseOne(String depId, ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flag = courseService.deleteCourseOne(depId);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"删除成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败"));
        }
        PageVo pageVo = courseService.getCourseList(null,pageInfo,username);
        modelAndView.setViewName("courseManage/courseManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现批量删除课程信息功能，并跳转至课程信息管理页面
     * @Param:  [java.lang.String[], org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteCourses")
    public ModelAndView deleteCourses(String[] ids,ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(ids);
        boolean flag = courseService.deleteCourses(ids);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"批量删除成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"批量删除失败"));
        }
        PageVo pageVo = courseService.getCourseList(null,pageInfo,username);
        modelAndView.setViewName("courseManage/courseManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

}
